﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;

namespace WxPayAPI
{
    public class PageGetAuthInfo : Page
    {
        public string msg = "";
        protected override void OnLoad(EventArgs e)
        {
            if (!IsPostBack)
            {
                JsApiPay jsApiPay = new JsApiPay(this);
                try
                {
                    //调用【网页授权获取用户信息】接口获取用户的openid和access_token
                    if (String.IsNullOrEmpty(jsApiPay.access_token) || String.IsNullOrEmpty(jsApiPay.openid))
                    {
                        jsApiPay.GetOpenidAndAccessTokenV2();
                    }
                    if (!string.IsNullOrEmpty(jsApiPay.openid))
                    {
                        string orderPageUrl = System.Web.Configuration.WebConfigurationManager.AppSettings["OrderPageUrl"];
                        string openid = jsApiPay.openid;
                        string url = orderPageUrl + "?openid=" + openid;
                        Log.Debug(this.GetType().ToString(), "Will Redirect to URL : " + url);
                        Response.Redirect(url);
                    }
                }
                catch (Exception ex)
                {
                    msg = @"<div class=""weui_msg"">
                                <div class=""weui_opr_area"">
                                    <span class=""weui_icon_warn""></span>
                                    微信支付服务繁忙，请重试
                                </div>
                            </div>";


                    Form.InnerHtml = msg;
                }
            }
            base.OnLoad(e);
        }
    }
}
